<?php
namespace App\Http\Controllers\Api;

use App\Requests\QiNiuRequest;
use App\Services\QiNiuService;
use Carbon\Carbon;
use Framework\BaseController;
use Kra8\Snowflake\Snowflake;


class QiNiuController extends BaseController
{

    /**
     * 获取token
     * @param QiNiuService $service
     * @return mixed
     */
    public function getQiNiuToken(QiNiuService $service)
    {
        return $this->retSuccess($service->getQiNiuToken());
    }


    /**
     * 创建应用
     * @param QiNiuRequest $request
     * @param QiNiuService $service
     * @return mixed
     */
    public function createApp(QiNiuRequest $request, QiNiuService $service)
    {
        $hub = $request->input('hub');
        $title = $request->input('title');
        $maxUsers = intval($request->input('maxUsers'));
        return $this->retSuccess($service->createApp($hub,$title,$maxUsers));
    }


    /**
     * 获取应用信息
     * @param QiNiuRequest $request
     * @param QiNiuService $service
     * @return mixed
     */
    public function getApp(QiNiuRequest $request, QiNiuService $service)
    {
        $appId = $request->input('appId');
        return $this->retSuccess($service->getApp($appId));
    }

    /**
     * 生成roomToken
     * @param QiNiuRequest $request
     * @param QiNiuService $service
     * @return mixed
     */
    public function appToken(QiNiuRequest $request, QiNiuService $service)
    {
        $appId = $request->input('appId');
        $roomName = $request->input('roomName');
        $userId = $request->input('userId');
        $expireAt = time() + 86400*72;
        $permission = $request->input('permission');
        return $this->retSuccess($service->appToken($appId, $roomName, $userId, $expireAt, $permission));
    }


    /**
     * 生成roomToken
     * @param QiNiuRequest $request
     * @param QiNiuService $service
     * @return mixed
     */
    public function listUser(QiNiuRequest $request, QiNiuService $service)
    {
        $appId = $request->input('appId');
        $roomName = $request->input('roomName');
        return $this->retSuccess($service->listUser($appId, $roomName));
    }

    


    /**
     * IM注册
     * @param QiNiuRequest $request
     * @param QiNiuService $service
     * @return mixed
     */
    public function imRegister(QiNiuRequest $request, QiNiuService $service)
    {
        $username = $request->input('username');
        return $this->retSuccess($service->imRegister($username));
    }

    /**
     * IM登录
     * @param QiNiuRequest $request
     * @param QiNiuService $service
     * @return mixed
     */
    public function getImuserToken(QiNiuService $service)
    {
        $user_id = $this->getAuthId();
        return $this->retSuccess($service->getImuserToken($user_id));
    }


    /**
     * IM登录
     * @param QiNiuRequest $request
     * @param QiNiuService $service
     * @return mixed
     */
    public function getTokenUser(QiNiuService $service)
    {
        return $this->retSuccess($service->getTokenUser());
    }



    /**
     * 获取IM用户信息
     * @param QiNiuRequest $request
     * @param QiNiuService $service
     * @return mixed
     */
    public function getImUserProfile(QiNiuRequest $request, QiNiuService $service)
    {
        $user_id = $request->input('user_id');
        return $this->retSuccess($service->getImUserProfile($user_id));
    }


    /**
     * 创建IM群聊
     * @param QiNiuService $service
     * @return mixed
     */
    public function createImGroup(QiNiuService $service)
    {
        $user_id = $this->getAuthId();
        return $this->retSuccess($service->createImGroup($user_id));
    }


    /**
     * 获取活跃房间
     * @param QiNiuRequest $request
     * @param QiNiuService $service
     * @return mixed
     */
    public function listActiveRooms(QiNiuRequest $request,QiNiuService $service)
    {
        $app_id = $request->input('app_id');
        return $this->retSuccess($service->listActiveRooms($app_id));
    }
















}
